package com.xyazm.product.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyazm.common.utils.PageUtils;
import com.xyazm.common.utils.Query;
import com.xyazm.product.dao.CategoryDao;
import com.xyazm.product.entity.CategoryEntity;
import com.xyazm.product.service.CategoryBrandRelationService;
import com.xyazm.product.service.CategoryService;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

	@Autowired
	private CategoryBrandRelationService categoryBrandRelationService;
	
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<CategoryEntity> page = this.page(new Query<CategoryEntity>().getPage(params),
				new QueryWrapper<CategoryEntity>());

		return new PageUtils(page);
	}

	@Override
	public List<CategoryEntity> listWithTree() {

		// 1.查询所有的分类数据
		List<CategoryEntity> selectList = baseMapper.selectList(null);
		// 2.找到所有的一级分类 (parentCid = 0)
		 List<CategoryEntity> level1Menus = selectList.stream()
		.filter(item -> item.getParentCid() == 0)
		.map(item -> {//获取当前一级分类的所有子分类
			item.setChildren(getChildrens(item, selectList));
			return item;
		}).sorted((menu1,menu2) -> {//对分类进行排序
			return (menu1.getSort() == null ? 0 : menu1.getSort()) 
					- (menu2.getSort() == null ? 0 : menu2.getSort());
		}).collect(Collectors.toList());

		return level1Menus;
	}

	
	/**
	 * 获取当前分类的子分类
	 * @Title: getChildrens
	 * @Description: 
	 * @Author xuyou
	 * @DateTime 2020年6月18日 下午2:04:17
	 * @param root
	 * @param all
	 * @return
	 */
	private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
		
		List<CategoryEntity> childrens = all.stream()
			.filter(item -> item.getParentCid().equals(root.getCatId()))
			.map(item -> {
				item.setChildren(getChildrens(item, all));
				return item;
			}).sorted((menu1,menu2) -> {
				return (menu1.getSort() == null ? 0 : menu1.getSort()) 
						- (menu2.getSort() == null ? 0 : menu2.getSort());
			}).collect(Collectors.toList());
		
		return childrens;
	}

	@Override
	public void removeMenuByIds(List<Long> menuIds) {
		//TODO 1.检查当前删除的菜单,是否被别的地方引用
		
		//逻辑批量删除(修改)
		baseMapper.deleteBatchIds(menuIds);
	}

	@Override
	public List<Long> findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);
        //将集合数据倒叙
		Collections.reverse(parentPath);
		return parentPath;
	}

	/**
	 * 递归寻找父分类id
	 * @Title: findParentPath
	 * @Description: 
	 * @Author xuyou
	 * @DateTime 2020年6月24日 下午4:30:29
	 * @param catelogId 当前节点分类id
	 * @param paths 存放分类id的集合
	 * @return
	 */
	private List<Long> findParentPath(Long catelogId, List<Long> paths) {
		paths.add(catelogId);
		CategoryEntity category = this.getById(catelogId);
		//如果当前节点还拥有父节点,则递归查询
		if (category.getParentCid() != 0) {
			findParentPath(category.getParentCid(),paths);
		}
		return paths;
	}

	@Transactional
	@Override
	public void updateDetail(CategoryEntity category) {
		this.baseMapper.updateById(category);
		if (StringUtils.isNotBlank(category.getName())) {
			categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
			//TODO 修改其他关联表的分类名称字段
		}
		
		
		
	}
	
	
	
}